local extension = Package:new("zhonger_heg_kann")
extension.extensionName = "zhonger_heg"
extension.game_modes_whitelist = {"zhonger_heg_mode"}
local H = require "packages/hegemony/util"
local U = require "packages/utility/utility"
local S = require "packages/seaborn/SeaFunc"
Fk:loadTranslationTable{
  ["zhonger_heg_kann"] = "中二扩展",
  ["ze_heg"] = "中二"
}

local huaxin = General(extension, "ze_heg__huaxin", "wei", 3)
local wanggui = fk.CreateTriggerSkill{
  name = "ze_heg__wanggui",
  mute = true,
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return (event == fk.Damage and player:getMark("wanggui-turn") == 0) or event == fk.Damaged
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets, prompt = {}, ""
    if event == fk.Damage then
      targets = table.map(table.filter(room.alive_players, function(p)
        return p.kingdom ~= player.kingdom end), Util.IdMapper)
      prompt = "#wanggui1-choose"
    else
      targets = table.map(table.filter(room.alive_players, function(p)
        return p.kingdom == player.kingdom end), Util.IdMappeir)
      prompt = "#wangguii-choose"
    end
    if #targets == 0 then return end
    local to = room:askForChoosePlayers(player, targets, 1, 1, prompt, self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    if event == fk.Damage then
      room:setPlayerMark(player, "wanggui-turn", 1)
      room:damage{
        from = player,
        to = to,
        damage = 1,
        skillName = self.name,
      }
    else
      to:drawCards(1, self.name)
    end
  end,
}
local ze_heg__xibing = fk.CreateActiveSkill{
  name = "ze_heg__xibing",
  anim_type = "control",
  card_num = 1,
  target_num = 1,
  prompt = "#ze_heg__xibing-invoke",
  interaction = function()
    local names = {}
    local mark = Self:getMark("@$ze_heg__xibing")
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if (card.type == Card.TypeBasic or card:isCommonTrick()) and not card.is_derived then
        if mark == 0 or (not table.contains(mark, card.trueName)) then
          table.insertIfNeed(names, card.name)
        end
      end
    end
    return UI.ComboBox {choices = names}
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and self.interaction.data and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and self.interaction.data and to_select ~= Self.id and Fk:currentRoom():getPlayerById(to_select).hp <= Self.hp
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:sendLog{
      type = "#ze_heg__xibing",
      from = player.id,
      arg = self.interaction.data,
    }
    local mark = player:getMark("@$ze_heg__xibing")
    if mark == 0 then mark = {} end
    table.insert(mark, Fk:cloneCard(self.interaction.data).trueName)
    room:setPlayerMark(player, "@$ze_heg__xibing", mark)
    player:showCards(effect.cards)
    if player.dead then return end
    local id = effect.cards[1]
    local choice = room:askForSkillInvoke(target, self.name, nil,
      "#ze_heg__xibing-choice:"..player.id.."::"..Fk:getCardById(id, true):toLogString()..":"..self.interaction.data)
    if choice then
      room:setCardMark(Fk:getCardById(id), "@@ze_heg__xibing", self.interaction.data)
      local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper),
        1, 1, "#ze_heg__xibing-choose", self.name, true)
      if #to > 0 then
        room:obtainCard(to[1], id, true, fk.ReasonGive)
      end
    else
      room:loseHp(target, 1, self.name)
    end
  end,
}
local ze_heg__xibing_record = fk.CreateTriggerSkill{
  name = "#ze_heg__xibing_record",

  refresh_events = {fk.AfterCardsMove},
  can_refresh = Util.TrueFunc,
  on_refresh = function(self, event, target, player, data)
  local room = player.room
    for _, move in ipairs(data) do
      if move.toArea ~= Card.PlayerHand and move.toArea ~= Card.Processing then
        for _, info in ipairs(move.moveInfo) do
          room:setCardMark(Fk:getCardById(info.cardId), "@@ze_heg__xibing", 0)
        end
      end
    end
  end,
}
local ze_heg__xibing_filter = fk.CreateFilterSkill{
  name = "#ze_heg__xibing_filter",
  card_filter = function(self, card, player)
    return card:getMark("@@ze_heg__xibing") ~= 0 and table.contains(player.player_cards[Player.Hand], card.id)
  end,
  view_as = function(self, card)
    return Fk:cloneCard(card:getMark("@@ze_heg__xibing"), card.suit, card.number)
  end,
}
ze_heg__xibing:addRelatedSkill(ze_heg__xibing_record)
ze_heg__xibing:addRelatedSkill(ze_heg__xibing_filter)
huaxin:addSkill(wanggui)
huaxin:addSkill(ze_heg__xibing)

Fk:loadTranslationTable{
  ["ze_heg__huaxin"] = "华歆",
  ["#ze_heg__huaxin"] = "渊清玉洁",
  ["designer:ze_heg__huaxin"] = "韩旭",
  ["illustrator:ze_heg__huaxin"] = "秋呆呆",
  ["ze_heg__wanggui"] = "望归",
  [":ze_heg__wanggui"] = "当你受到伤害后，你可令一名与你势力相同的角色摸一张牌。",
  ["#wangguii-choose"] = "望归：你可以令一名势力与你相同的角色摸一张牌",
  ["ze_heg__xibing"] = "草诏",
  [":ze_heg__xibing"] = "出牌阶段限一次，你可展示一张手牌并声明一种未以此法声明过的基本牌或普通锦囊牌，令一名体力值不大于你的其他角色选择一项："..
  "令此牌视为你声明的牌，或其失去1点体力。然后若此牌声明成功，你可以将之交给一名其他角色。",
  ["#ze_heg__xibing-invoke"] = "草诏：选择要声明的牌，然后选择展示的手牌和目标角色",
  ["#ze_heg__xibing"] = "%from 声明了 %arg",
  ["#ze_heg__xibing-choice"] = "草诏：令 %src 将%arg视为【%arg2】，或点“取消”你失去1点体力",
  ["@$ze_heg__xibing"] = "草诏",
  ["@@ze_heg__xibing"] = "草诏",
  ["#ze_heg__xibing-choose"] = "草诏：你可以将这张牌交给一名其他角色",
  ["#ze_heg__xibing_filter"] = "草诏",
  ["$ze_heg__wanggui1"] = "存志太虚，安心玄妙。",
  ["$ze_heg__wanggui2"] = "礼法有度，良德才略。",
  ["$ze_heg__xibing1"] = "草诏所宣，密勿从事。",
  ["$ze_heg__xibing2"] = "惩恶扬功，四方之纲。",
  ["~ze_heg__huaxin"] = "大举发兵，劳民伤国。",
}

local dongzhao = General(extension, "ze_heg__dongzhao", "wei", 3)
local yingjia = fk.CreateTriggerSkill{
  name = "ze_heg__yingjia",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 998, function(e) 
      local use = e.data[1]
      return use.from == player.id and use.card.type == Card.TypeTrick
    end, Player.HistoryTurn)
    if #events > 0 then
      local usedCardNames = {}     
      table.forEach(events, function(e)
        table.insertIfNeed(usedCardNames, e.data[1].card.name)
      end)
      return #events > #usedCardNames
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player:usedSkillTimes(self.name, Player.HistoryRound) > 0 then return false end
    local plist, cid = player.room:askForChooseCardAndPlayers(player, table.map(player.room.alive_players, Util.IdMapper), 1, 1, ".|.|.|hand", "#ze_heg__yingjia-target", self.name, true) --但是没判断可不可以弃置
    if #plist > 0 then
      self.cost_data = {plist[1], cid}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = self.cost_data[1]
    room:throwCard(self.cost_data[2], self.name, player)
    room:getPlayerById(to):gainAnExtraTurn()
  end
}

local ze_heg__miaolue = fk.CreateTriggerSkill{
  name = "ze_heg__miaolue",
  events = {fk.Damaged},
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if player:usedSkillTimes(self.name) > 0 then return false end
    return event == fk.GameStart or (target == player and not player.dead)
  end,
  on_trigger = function(self, event, target, player, data)
    if event == fk.Damaged then
      self.cancel_cost = false
      for i = 1, data.damage do
        if self.cancel_cost then break end
        self:doCost(event, target, player, data)
      end
    else
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GameStart then
      return true
    else
      local choice = player.room:askForChoice(player, {"ze_heg__miaolue_underhanding", "ze_heg__miaolue_zhinang", "Cancel"}, self.name)
      if choice ~= "Cancel" then
        self.cost_data = choice
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local cids = {}
      for _, cid in ipairs(Fk:getAllCardIds()) do
        if Fk:getCardById(cid).name == "mama" and room:getCardArea(cid) == Card.Void then
          table.insert(cids, cid)
          if #cids == 2 then break end
        end
      end
      if #cids > 0 then
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(cids)
        room:obtainCard(player, dummy, false, fk.ReasonPrey)
      end
    else
      if self.cost_data == "ze_heg__miaolue_underhanding" then
        player:drawCards(2, self.name)
      else
        local choice = room:askForChoice(player, {"dismantlement", "nullification", "ex_nihilo"}, self.name, "#ze_heg__miaolue-ask")
        local id = room:getCardsFromPileByRule(choice, 1, "allPiles")
        if #id > 0 then
          room:obtainCard(player, id[1], false, fk.ReasonPrey)
        end
      end
    end
  end,
}
local ze_heg__miaolue_destruct = fk.CreateTriggerSkill{
  name = "#ze_heg__miaolue_destruct",
  refresh_events = {fk.BeforeCardsMove},
  can_refresh = Util.TrueFunc,
  on_refresh = function(self, event, target, player, data)
    local hold_areas = {Card.Processing, Card.Void, Card.PlayerHand}
    local mirror_moves = {}
    local ids = {}
    for _, move in ipairs(data) do
      if not table.contains(hold_areas, move.toArea) then
        local move_info = {}
        local mirror_info = {}
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          if Fk:getCardById(id).name == "underhanding" then
            table.insert(mirror_info, info)
            table.insert(ids, id)
          else
            table.insert(move_info, info)
          end
        end
        if #mirror_info > 0 then
          move.moveInfo = move_info
          local mirror_move = table.clone(move)
          mirror_move.to = nil
          mirror_move.toArea = Card.Void
          mirror_move.moveInfo = mirror_info
          table.insert(mirror_moves, mirror_move)
        end
      end
    end
    if #ids > 0 then
      player.room:sendLog{
        type = "#destructDerivedCards",
        card = ids,
      }
    end
    table.insertTable(data, mirror_moves)
  end,
}
ze_heg__miaolue:addRelatedSkill(ze_heg__miaolue_destruct)


dongzhao:addSkill(yingjia)
dongzhao:addSkill(ze_heg__miaolue)
Fk:loadTranslationTable{
  ["ze_heg__dongzhao"] = "董昭",
  ["ze_heg__yingjia"] = "迎驾",
  [":ze_heg__yingjia"] = "每轮限一次，一名角色的回合结束时，若你于此回合内使用过至少两张同名锦囊牌，你可弃置一张手牌并选择一名角色，其获得一个额外的回合。",
  ["#ze_heg__yingjia-target"] = "迎驾：你可弃置一张手牌并选择一名角色，其获得一个额外的回合",
  ["$ze_heg__yingjia1"] = "大魏当兴，吾主可王。",
  ["$ze_heg__yingjia2"] = "身加九锡，当君不让。",
  ["ze_heg__miaolue_underhanding"] = "摸两张牌",
  ["ze_heg__miaolue_zhinang"] = "从牌堆或弃牌堆获得一张你指定的智囊",
  ["#ze_heg__miaolue-ask"] = "妙略：选择一种“智囊”，从牌堆或弃牌堆获得一张",
  ["ze_heg__miaolue"] = "妙略",
  [":ze_heg__miaolue"] = "每回合限一次，当你受到伤害后，你可选择：1. 摸两张牌；2. 从牌堆或弃牌堆获得一张你指定的智囊。" ,
  ["$ze_heg__miaolue1"] = "行略于先，未雨绸缪。",
  ["$ze_heg__miaolue2"] = "先见梧叶，而后知秋。",
  ["~ze_heg__dongzhao"] = "昭，一心向魏，绝无二心……",
}

local ze_heg__jianggan = General(extension, "ze_heg__jianggan", "wei", 3)
local ze_heg__daoshu = fk.CreateTriggerSkill{
  name = "ze_heg__daoshu",
  mute = true,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Play and
      not target.dead and not target:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#ze_heg__daoshu-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name, 1)
    room:notifySkillInvoked(player, self.name, "control")
    room:doIndicate(player.id, {target.id})
    room:useVirtualCard("jink", nil, target, target, self.name, false)
    if player.dead or target.dead then return end
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card.type == Card.TypeBasic then
        table.insertIfNeed(names, card.name)
      end
    end
    local choice = room:askForChoice(target, names, self.name, "#ze_heg__daoshu-declare:"..player.id)
    room:doBroadcastNotify("ShowToast", Fk:translate(target.general)..Fk:translate("#ze_heg__daoshu")..Fk:translate(choice))
    local yes = room:askForChoice(player, {"yes", "no"}, self.name, "#ze_heg__daoshu-choice::"..target.id..":"..choice)
    local right = table.find(target:getCardIds("h"), function(id) return Fk:getCardById(id).trueName == choice end)
    if ((yes == "yes" and right) or (yes == "no" and not right)) then
      if not target:isKongcheng() then
        player:broadcastSkillInvoke(self.name, 2)
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(table.random(target:getCardIds("h"), 2))
        room:moveCardTo(dummy, Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, false, player.id)
      end
    else
      player:broadcastSkillInvoke(self.name, 3)
    end
  end,
}
local ze_heg__daizui = fk.CreateTriggerSkill{
  name = "ze_heg__daizui",
  anim_type = "defensive",
  frequency = Skill.Limited,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.damage >= player.hp and
      player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.from and not data.from.dead and data.card then
      local subcards = data.card:isVirtual() and data.card.subcards or {data.card.id}
      if #subcards > 0 and table.every(subcards, function(id) return room:getCardArea(id) == Card.Processing end) then
        room:doIndicate(player.id, {data.from.id})
        player:addToPile("ze_heg__jianggan_shi", data.card, true, self.name)
        local turn = room.logic:getCurrentEvent():findParent(GameEvent.Round)
        if turn ~= nil then
          turn:addCleaner(function()
            if not data.from.dead and #data.from:getPile("ze_heg__jianggan_shi") > 0 then
              local dummy = Fk:cloneCard("dilu")
              dummy:addSubcards(data.from:getPile("ze_heg__jianggan_shi"))
              room:moveCardTo(dummy, Card.PlayerHand, data.from, fk.ReasonJustMove, self.name, nil, true, data.from.id)
            end
          end)
        end
      end
    end
    return true
  end,
}

ze_heg__jianggan:addSkill(ze_heg__daoshu)
ze_heg__jianggan:addSkill(ze_heg__daizui)
Fk:loadTranslationTable{
  ["ze_heg__jianggan"] = "蒋干",
  ["ze_heg__daoshu"] = "盗书",
  [":ze_heg__daoshu"] = "每轮限一次，一名角色的出牌阶段开始时，若其有手牌，你可以令其视为使用一张【酒】，并令其声明其手牌中有一种基本牌，你判断真假。"..
  "若判断正确，则你随机获得其两张手牌。",
  ["#ze_heg__daoshu-invoke"] = "盗书：令 %dest 视为使用【酒】，然后猜测其手牌中是否有其声明的基本牌，若猜对你获得其两张手牌",
  ["#ze_heg__daoshu-declare"] = "盗书：声明你有某种基本牌，令 %src 猜测",
  ["#ze_heg__daoshu"] = "声明其手牌中有 ",
  ["#ze_heg__daoshu-choice"] = "盗书：猜测 %dest 手中是否有【%arg】？若猜对你获得其两张手牌",
  ["ze_heg__daizui"] = "戴罪",
  [":ze_heg__daizui"] = "限定技，当你受到致命伤害时，你可以防止之，然后将造成伤害的牌置于伤害来源的武将牌上，称为“释”。本回合结束后，其获得所有“释”。",
  ["ze_heg__jianggan_shi"] = "释",
  ["$ze_heg__daizui1"] = "望丞相权且记过，容干将功折罪啊！",
  ["$ze_heg__daizui2"] = "干，谢丞相不杀之恩！",
  ["$ze_heg__daoshu1"] = "得此文书，丞相定可高枕无忧。",
  ["$ze_heg__daoshu2"] = "让我看看，这是什么机密。",
  ["$ze_heg__daoshu3"] = "吾只观雅规，而非说客。",
  ["~ze_heg__jianggan"] = "丞相，再给我一次机会啊！",
}

local ze_heg__xuelingyun = General(extension, "ze_heg__xuelingyun", "wei", 3, 3, General.Female)
local ze_heg__xialei = fk.CreateTriggerSkill{
  name = "ze_heg__xialei",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player:usedSkillTimes(self.name, Player.HistoryGame) > 0 then return false end
    local room = player.room
    local move_event = room.logic:getCurrentEvent()
    local parent_event = move_event.parent
    local card_ids = {}
    if parent_event ~= nil then
      if parent_event.event == GameEvent.UseCard or parent_event.event == GameEvent.RespondCard then
        local parent_data = parent_event.data[1]
        if parent_data.from == player.id then
          card_ids = room:getSubcardsByRule(parent_data.card)
        end
      elseif parent_event.event == GameEvent.Pindian then
        local pindianData = parent_event.data[1]
        if pindianData.from == player then
          card_ids = room:getSubcardsByRule(pindianData.fromCard)
        else
          for toId, result in pairs(pindianData.results) do
            if player.id == toId then
              card_ids = room:getSubcardsByRule(result.toCard)
              break
            end
          end
        end
      end
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
            Fk:getCardById(info.cardId).suit == Card.Heart then
              return true
            end
          end
        elseif #card_ids > 0 then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) and
            Fk:getCardById(info.cardId).color == Card.Red then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = room:getNCards(3 - player:getMark("ze_heg__xialei-turn"))
    if #ids == 1 then
      room:moveCards({
        ids = ids,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = self.name,
      })
    else
      local to_return, choice = U.askforChooseCardsAndChoice(player, ids, {"ze_heg__xialei_top", "ze_heg__xialei_bottom"}, self.name, "#ze_heg__xialei-chooose")
      local moveInfos = {
        ids = to_return,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = self.name,
      }
      table.removeOne(ids, to_return[1])
      if #ids > 0 then
        if choice == "ze_heg__xialei_top" then
          for i = #ids, 1, -1 do
            table.insert(room.draw_pile, 1, ids[i])
          end
        else
          for _, id in ipairs(ids) do
            table.insert(room.draw_pile, id)
          end
        end
      end
      room:moveCards(moveInfos)
    end
    room:addPlayerMark(player, "ze_heg__xialei-turn", 1)
  end,
}
local ze_heg__anzhi = fk.CreateTriggerSkill{
  name = "ze_heg__anzhi",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getAlivePlayers(), Util.IdMapper),
      1, 1, "#ze_heg__anzhi-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
      to:drawCards(1, self.name)
      player:drawCards(1, self.name)
  end,
}
ze_heg__xuelingyun:addSkill(ze_heg__xialei)
ze_heg__xuelingyun:addSkill(ze_heg__anzhi)
ze_heg__xuelingyun:addCompanions("zhonger_st_heg__caopi")
Fk:loadTranslationTable{
  ["ze_heg__xuelingyun"] = "薛灵芸",
  ["#ze_heg__xuelingyun"] = "霓裳缀红泪",
  ["designer:ze_heg__xuelingyun"] = "懵萌猛梦",
  ["illustrator:ze_heg__xuelingyun"] = "Jzeo",
  ["ze_heg__xialei"] = "霞泪",
  [":ze_heg__xialei"] = "每回合限一次，当你的红桃牌进入弃牌堆后，你可观看牌堆顶的三张牌，然后你获得一张并可将其他牌置于牌堆底。",
  ["#ze_heg__xialei-chooose"] = "霞泪：选择一张卡牌获得",
  ["ze_heg__xialei_top"] = "将剩余牌置于牌堆顶",
  ["ze_heg__xialei_bottom"] = "将剩余牌置于牌堆底",
  ["ze_heg__anzhi"] = "暗织",
  [":ze_heg__anzhi"] = "当你受到伤害后，你可以与一名角色各摸一张牌。",
  ["#ze_heg__anzhi-choose"] = "暗织：你可以选择一名角色",
  ["$ze_heg__xialei1"] = "采霞揾晶泪，沾我青衫湿。",
  ["$ze_heg__xialei2"] = "登车入宫墙，垂泪凝如瑙。",
  ["$ze_heg__anzhi1"] = "深闱行彩线，唯手熟尔。",
  ["$ze_heg__anzhi2"] = "星月独照人，何谓之暗？",
  ["~ze_heg__xuelingyun"] = "寒月隐幕，难作衣裳。",
}

local zhangchunhua = General(extension, "ze_heg__zhangchunhua", "wei", 3, 3, General.Female)
local ze_heg__jueqing = fk.CreateActiveSkill{
  name = "ze_heg__jueqing",
  target_num = 1,
  min_card_num = 2,
  max_card_num = 2,
  prompt = function(self, card, selected_targets)
    if self.interaction.data == "ze_heg__jueqing_discard" then
      return "#ze_heg__jueqing1-active"
    else
      return "#ze_heg__jueqing2-active"
    end
  end,
  interaction = function()
    return UI.ComboBox {
      choices = {"ze_heg__jueqing_discard"}
    }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return self.interaction.data == "ze_heg__jueqing_discard" and #selected < 2 and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id and (#selected_cards > 0 or self.interaction.data ~= "ze_heg__jueqing_discard")
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local n = #effect.cards
    if n > 0 then
      room:throwCard(effect.cards, self.name, player, player)
      if target.dead then return end
      room:loseHp(target, 1, self.name)
    else
      n = 1
      player:drawCards(1, self.name)
      if target.dead then return end
      room:askForDiscard(target, 1, 1, true, self.name, false)
    end
  end,
}
local ze_heg__shangshi = fk.CreateTriggerSkill{
  name = "ze_heg__shangshi",
  anim_type = "drawcard",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
        return target == player
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(player:getLostHp(), self.name)
  end,
}
zhangchunhua:addSkill(ze_heg__jueqing)
zhangchunhua:addSkill(ze_heg__shangshi)
zhangchunhua:addCompanions("zhonger_st_heg__simayi")
Fk:loadTranslationTable{
  ["ze_heg__zhangchunhua"] = "张春华",
  ["#ze_heg__zhangchunhua"] = "冷血皇后",
  ["illustrator:ze_heg__zhangchunhua"] = "七兜豆",
  ["ze_heg__jueqing"] = "绝情",
  [":ze_heg__jueqing"] = "出牌阶段限一次，你可以弃置两张牌，令一名其他角色失去1点体力。",
  ["ze_heg__jueqing_discard"] = "弃置两张牌",
  ["#ze_heg__jueqing1-active"] = "发动 绝情，弃置两张牌",
  ["@@ze_heg__jueqing"] = "绝情",
  ["#ze_heg__jueqing_delay"] = "绝情",
  ["ze_heg__shangshi"] = "伤逝",
  [":ze_heg__shangshi"] = "锁定技，当你受到伤害后，你摸X张牌（X为你已损失的体力值）。",
  ["$ze_heg__jueqing1"] = "不知情之所起，亦不知情之所终。",
  ["$ze_heg__jueqing2"] = "唯有情字最伤人！",
  ["$ze_heg__shangshi1"] = "半生韶华随流水，思君不见撷落花。",
  ["$ze_heg__shangshi2"] = "西风知我意，送我三尺秋。",
  ["~ze_heg__zhangchunhua"] = "我何为也？竟称可憎之老物……",
}





local liuba = General(extension, "ze_heg__liuba", "shu", 3)
local ze_heg__zhubi = fk.CreateTriggerSkill{
  name = "ze_heg__zhubi",
  anim_type = "support",
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.from and data.from ~= player.id and
      U.isOnlyTarget(player, data, event) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and
      table.find(player.room.alive_players, function(p) return not p:isNude() end)
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(table.filter(player.room.alive_players, function(p)
      return not p:isNude() end), Util.IdMapper),
      1, 1, "#ze_heg__zhubi-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local cards = room:askForCard(to, 1, 1, true, self.name, false, ".", "#ze_heg__zhubi-card:"..player.id)
    local card = Fk:getCardById(cards[1])
    room:moveCards({
      ids = cards,
      from = to.id,
      toArea = Card.DiscardPile,
      skillName = self.name,
      moveReason = fk.ReasonPutIntoDiscardPile,
      proposer = to.id
    })
    room:sendLog{
      type = "#RecastBySkill",
      from = to.id,
      card = cards,
      arg = self.name,
    }
    if card.suit == Card.Heart or card.type == Card.TypeTrick then
      to:drawCards(2, self.name)
    else
      to:drawCards(2, self.name)
    end
  end,
}
local ze_heg__liuzhuan = fk.CreateTriggerSkill{
  name = "ze_heg__liuzhuan",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    local room = player.room
    local current = room.current
    if current == player or current.phase == Player.NotActive then return false end
    local toMarked, toObtain = {}, {}
    local id
    for _, move in ipairs(data) do
      if current.phase ~= Player.Draw and move.to == current.id and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          id = info.cardId
          if room:getCardArea(id) == Card.PlayerHand and room:getCardOwner(id) == current then
            table.insert(toMarked, id)
          end
        end
      end
      local mark = U.getMark(player, "ze_heg__liuzhuan_record-turn")
      if move.toArea == Card.DiscardPile and #mark > 0 then
        for _, info in ipairs(move.moveInfo) do
          id = info.cardId
          --for stupid manjuan
          if info.fromArea ~= Card.DiscardPile and table.removeOne(mark, id) and room:getCardArea(id) == Card.DiscardPile then
            table.insert(toObtain, id)
          end
        end
      end
      toObtain = U.moveCardsHoldingAreaCheck(room, toObtain)
      if #toMarked > 0 or #toObtain > 0 then
        self.cost_data = {toMarked, toObtain}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toMarked = table.simpleClone(self.cost_data[1])
    local toObtain = table.simpleClone(self.cost_data[2])
    local mark = U.getMark(player, "ze_heg__liuzhuan_record-turn")
    table.insertTableIfNeed(mark, toMarked)
    room:setPlayerMark(player, "ze_heg__liuzhuan_record-turn", mark)
    for _, id in ipairs(toMarked) do
      room:setCardMark(Fk:getCardById(id), "@@ze_heg__liuzhuan-inhand-turn", 1)
    end
  end,
  refresh_events = {fk.AfterCardsMove, fk.Death},
  can_refresh = function(self, event, target, player, data)
    if event == fk.Death and player ~= target then return false end
    return #U.getMark(player, "ze_heg__liuzhuan_record-turn") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = U.getMark(player, "ze_heg__liuzhuan_record-turn")
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.to ~= room.current.id and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip) then
          for _, info in ipairs(move.moveInfo) do
            table.removeOne(mark, info.cardId)
          end
        end
      end
      room:setPlayerMark(player, "ze_heg__liuzhuan_record-turn", mark)
    elseif event == fk.Death then
      local card
      for _, id in ipairs(mark) do
        card = Fk:getCardById(id)
        if card:getMark("@@ze_heg__liuzhuan-inhand-turn") > 0 and table.every(room.alive_players, function (p)
          return not table.contains(U.getMark(p, "ze_heg__liuzhuan_record-turn"), id)
        end) then
          room:setCardMark(card, "@@ze_heg__liuzhuan-inhand-turn", 0)
        end
      end
    end
  end,
}
local ze_heg__liuzhuan_prohibit = fk.CreateProhibitSkill{
  name = "#ze_heg__liuzhuan_prohibit",
  is_prohibited = function(self, from, to, card)
    if not to:hasSkill(ze_heg__liuzhuan) then return false end
    local mark = U.getMark(to, "ze_heg__liuzhuan_record-turn")
    if #mark == 0 then return false end
    for _, id in ipairs(Card:getIdList(card)) do
      if table.contains(mark, id) and table.contains(from:getCardIds("he"), id) then
        return true
      end
    end
  end,
}
ze_heg__liuzhuan:addRelatedSkill(ze_heg__liuzhuan_prohibit)
liuba:addSkill(ze_heg__zhubi)
liuba:addSkill(ze_heg__liuzhuan)
Fk:loadTranslationTable{
  ["ze_heg__liuba"] = "刘巴",
  ["#ze_heg__liuba"] = "清河一鲲",
  ["illustrator:ze_heg__liuba"] = "匠人绘",
  ["ze_heg__zhubi"] = "铸币",
  [":ze_heg__zhubi"] = "每回合限一次，当你成为其他角色使用牌的唯一目标后，你可以令一名角色重铸一张牌，然后其摸一张牌。",
  ["#ze_heg__zhubi-choose"] = "统度：你可以令一名角色重铸一张牌，然后其摸一张牌",
  ["#ze_heg__zhubi-card"] = "统度：重铸一张牌，并摸一张牌",
  ["ze_heg__liuzhuan"] = "流转",
  [":ze_heg__liuzhuan"] = "锁定技，其他角色的回合内，其于摸牌阶段外获得的牌无法对你使用。",
  ["@@ze_heg__liuzhuan-inhand-turn"] = "流转",
  ["$ze_heg__zhubi1"] = "铸币平市，百货可居。",
  ["$ze_heg__zhubi2"] = "做钱直百，府库皆实。",
  ["$ze_heg__liuzhuan1"] = "身似浮萍，随波逐流。",
  ["$ze_heg__liuzhuan2"] = "辗转四方，宦游八州。",
  ["~ze_heg__liuba"] = "竹蕴于林，风必摧之。",
}

local zongyu = General(extension, "ze_heg__zongyu", "shu", 3)
zongyu:addSkill("os__zhibian")
zongyu:addSkill("os__yuyan")
Fk:loadTranslationTable{
  ["ze_heg__zongyu"] = "宗预",
  ["#ze_heg__zongyu"] = "九酝鸿胪",
  ["designer:ze_heg__zongyu"] = "官方",
  ["illustrator:ze_heg__zongyu"] = "铁杵文化",
  ["~ze_heg__zongyu"] = "吾年逾七十，唯少一死耳……",
}

local xujing = General(extension, "ze_heg__xujing", "shu", 3)  -- 许靖
xujing:addSkill("os__boming")
xujing:addSkill("os__ejian")
S.generalInfo(xujing, "许靖", "璞玉有瑕", "官方", "官方", "官方", "官方", "时人如江鲫，所逐者功利尔。")
local guansuo = General(extension, "ze_heg__guansuo", "shu", 4) -- 关索
local ze_heg__zhengnan = fk.CreateTriggerSkill{
  name = "ze_heg__zhengnan",
  anim_type = "drawcard",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and (player:getMark(self.name) == 0 or not table.contains(player:getMark(self.name), target.id))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark(self.name)
    if mark == 0 then mark = {} end
    table.insert(mark, target.id)
    room:setPlayerMark(player, self.name, mark)
    local choices = {"wusheng", "dangxian", "ld__zhiman"}
    for i = 3, 1, -1 do
      if player:hasSkill(choices[i], true) then
        table.removeOne(choices, choices[i])
      end
    end
    if #choices > 0 then
      local choice = room:askForChoice(player, choices, self.name, "#zhengnan-choice", true)
      room:handleAddLoseSkills(player, choice, nil)
    end
  end,
}
guansuo:addSkill(ze_heg__zhengnan)
guansuo:addSkill("xiefang")
guansuo:addRelatedSkill("wusheng")
guansuo:addRelatedSkill("dangxian")
guansuo:addRelatedSkill("ld__zhiman")
Fk:loadTranslationTable{
  ["ze_heg__guansuo"] = "关索",
  ["#ze_heg__guansuo"] = "倜傥孑侠",
  ["illustrator:ze_heg__guansuo"] = "官方",
  ["ze_heg__zhengnan"] = "征南",
  [":ze_heg__zhengnan"] = "每名角色限一次，当一名角色进入濒死状态时，你可以选择获得下列技能中的一个："..
  "〖武圣〗，〖当先〗和〖制蛮〗。",
  ["$ze_heg__zhengnan1"] = "索全凭丞相差遣，万死不辞！",
  ["$ze_heg__zhengnan2"] = "末将愿承父志，随丞相出征！",
  ["$wusheng_ze_heg__guansuo"] = "逆贼，可识得关氏之勇？",
  ["$dangxian_ze_heg__guansuo"] = "各位将军，且让小辈先行出战！",
  ["$ld__zhiman_ze_heg__guansuo"] = "蛮夷可抚，不可剿！",
  ["~ze_heg__guansuo"] = "只恨天下未平，空留遗志。",
}

local ze_heg__guanyinping = General(extension, "ze_heg__guanyinping", "shu", 3, 3, General.Female)
ze_heg__guanyinping:addSkill("xueji")
ze_heg__guanyinping:addSkill("huxiao")
Fk:loadTranslationTable{
  ["ze_heg__guanyinping"] = "关银屏",
  ["#ze_heg__guanyinping"] = "武姬",
  ["designer:ze_heg__guanyinping"] = "韩旭",
  ["illustrator:ze_heg__guanyinping"] = "木美人",
  ["~ze_heg__guanyinping"] = "父亲，你来救我了吗……",
}

local zhouchu = General(extension, "ze_heg__zhouchu", "wu", 4)
local ze_heg__chuhai = fk.CreateActiveSkill{
  name = "ze_heg__chuhai",
  anim_type = "offensive",
  prompt = "#ze_heg__chuhai-active",
  frequency = Skill.Quest,
  mute = true,
  max_card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:getQuestSkillState(self.name)
  end,
  card_filter = function() return false end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and Self:canPindian(Fk:currentRoom():getPlayerById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:notifySkillInvoked(player, self.name)
    player:broadcastSkillInvoke(self.name, 1)
    if player.dead or target.dead or not player:canPindian(target) then return false end
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id].winner == player then
      local cards = target.player_cards[Player.Hand]
      if #cards > 0 then
        U.viewCards(player, cards, self.name)
        local types = {}
        for _, id in ipairs(cards) do
          table.insertIfNeed(types, Fk:getCardById(id):getTypeString())
        end

        local toObtain = {}
        for _, type_name in ipairs(types) do
          local randomCard = room:getCardsFromPileByRule(".|.|.|.|.|" .. type_name, 1)
          if #randomCard == 0 then
            randomCard = room:getCardsFromPileByRule(".|.|.|.|.|" .. type_name, 1, "discardPile")
          end
          if #randomCard > 0 then
            table.insert(toObtain, randomCard[1])
          end
        end
      end
      room:addPlayerMark(target, "@@ze_heg__chuhai-phase")
      local targetRecorded = type(player:getMark("ze_heg__chuhai_target-phase")) == "table" and player:getMark("ze_heg__chuhai_target-phase") or {}
      table.insertIfNeed(targetRecorded, target.id)
      room:setPlayerMark(player, "ze_heg__chuhai_target-phase", targetRecorded)
    end
  end,
}
local ze_heg__chuhai_trigger = fk.CreateTriggerSkill{
  name = "#ze_heg__chuhai_trigger",
  events = {fk.AfterCardsMove, fk.PindianResultConfirmed},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(ze_heg__chuhai.name) or player:getQuestSkillState(ze_heg__chuhai.name) then return false end
    if event == fk.AfterCardsMove and #player.player_cards[Player.Equip] > 2 then
      for _, move in ipairs(data) do
        if move.to and move.to == player.id and move.toArea == Player.Equip then
          return true
        end
      end
    elseif event == fk.PindianResultConfirmed then
      if data.from == player and data.winner ~= player and data.fromCard.number < 7 then
        local parentPindianEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.Pindian, true)
        if parentPindianEvent then
          local pindianData = parentPindianEvent.data[1]
          return pindianData.reason == ze_heg__chuhai.name
        end
      end
    end
  end,
  on_cost = function() return true end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, ze_heg__chuhai.name, "special")
      player:broadcastSkillInvoke(ze_heg__chuhai.name, 2)
      room:updateQuestSkillState(player, ze_heg__chuhai.name, false)
      room:handleAddLoseSkills(player, "-xianghai|ze_heg__zhangming")
    elseif event == fk.PindianResultConfirmed then
      room:notifySkillInvoked(player, ze_heg__chuhai.name, "negative")
      player:broadcastSkillInvoke(ze_heg__chuhai.name, 3)
      room:updateQuestSkillState(player, ze_heg__chuhai.name, true)
    end
  end,
}
local ze_heg__chuhai_delay = fk.CreateTriggerSkill{
  name = "#ze_heg__chuhai_delay",
  mute = true,
  events = {fk.Damage, fk.PindianCardsDisplayed},
  can_trigger = function(self, event, target, player, data)
    if event == fk.Damage and player == target then
      return type(player:getMark("ze_heg__chuhai_target-phase")) == "table" and table.contains(player:getMark("ze_heg__chuhai_target-phase"), data.to.id)
    elseif event == fk.PindianCardsDisplayed then
      return data.reason == ze_heg__chuhai.name and data.from == player and #player.player_cards[Player.Equip] < 4
    end
  end,
  on_cost = function() return true end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      local types = table.filter({Card.SubtypeWeapon, Card.SubtypeArmor, Card.SubtypeDefensiveRide,
      Card.SubtypeOffensiveRide, Card.SubtypeTreasure}, function (type_name) return not player:getEquipment(type_name) end)
      if #types == 0 then return false end
      local cards1, cards2 = {}, {}
      for i = 1, #types, 1 do
        table.insert(cards1, {})
        table.insert(cards2, {})
      end
      for i = 1, #room.draw_pile, 1 do
        local card = Fk:getCardById(room.draw_pile[i])
        if card.type == Card.TypeEquip and table.contains(types, card.sub_type) then
          table.insert(cards1[table.indexOf(types, card.sub_type)], card.id)
        end
      end
      for i = 1, #room.discard_pile, 1 do
        local card = Fk:getCardById(room.discard_pile[i])
        if card.type == Card.TypeEquip and table.contains(types, card.sub_type) then
          table.insert(cards2[table.indexOf(types, card.sub_type)], card.id)
        end
      end

      for i = 1, #types, 1 do
        if #cards1[i] > 0 then
          room:moveCards({
            ids = {table.random(cards1[i])},
            to = player.id,
            toArea = Card.PlayerEquip,
            moveReason = fk.ReasonPut,
          })
          break
        end
        if #cards2[i] > 0 then
          room:moveCards({
            ids = {table.random(cards2[i])},
            to = player.id,
            toArea = Card.PlayerEquip,
            moveReason = fk.ReasonPut,
          })
          break
        end
      end
    end
  end,
}
local ze_heg__zhangming = fk.CreateViewAsSkill{
  name = "ze_heg__zhangming",
  pattern = ".|.|.|.|.|basic",
  prompt = "#ze_heg__zhangming-viewas",
  interaction = function()
    local mark = U.getMark(Self, "ze_heg__zhangming-round")
    local all_names = U.getAllCardNames("b")
    local names = table.filter(U.getViewAsCardNames(Self, "ze_heg__zhangming", all_names), function (name)
      local card = Fk:cloneCard(name)
      return not table.contains(mark, card.trueName)
    end)
    if #names > 0 then
      return UI.ComboBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, to_select, selected)
    if 1==1 then
      if #selected == 0 then
        return true
      elseif #selected == 1 then
        return Fk:getCardById(to_select).color == Fk:getCardById(selected[1]).color
      else
        return false
      end
    end
  end,
  view_as = function(self, cards)
    if #cards ~= 2 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    card:addSubcards(cards)
    return card
  end,
  enabled_at_response = function(self, player, response)
    return not response
  end,
}
ze_heg__chuhai:addRelatedSkill(ze_heg__chuhai_trigger)
ze_heg__chuhai:addRelatedSkill(ze_heg__chuhai_delay)
zhouchu:addSkill("xianghai")
zhouchu:addSkill(ze_heg__chuhai)
zhouchu:addRelatedSkill(ze_heg__zhangming)
Fk:loadTranslationTable{
  ["ze_heg__zhouchu"] = "周处",
  ["#ze_heg__zhouchu"] = "英情天逸",
  ["illustrator:ze_heg__zhouchu"] = "枭瞳",
  ["ze_heg__chuhai"] = "除害",
  [":ze_heg__chuhai"] = "使命技，出牌阶段限一次，你可以与一名角色拼点。若你赢："..
  "你观看其手牌，且你于此阶段对其造成伤害后，将牌堆或弃牌堆中一张你空置装备栏对应类型的装备牌置入你对应的装备区。<br>\
  <strong>成功</strong>：当一张装备牌进入你的装备区后，若你的装备区有不少于3张装备，则你获得〖彰名〗，失去〖乡害〗。<br>\
  <strong>失败</strong>：若你于使命达成前，你使用〖除害〗拼点没赢，且你的拼点结果不大于6点，则使命失败。",
  ["ze_heg__zhangming"] = "彰名",
  [":ze_heg__zhangming"] = "你可以将两张颜色相同的手牌当一张基本牌使用。",
  ["#ze_heg__chuhai_trigger"] = "除害",
  ["#ze_heg__chuhai_delay"] = "除害",
  ["#ze_heg__chuhai-active"] = "发动除害，选择与你拼点的角色",
  ["@@ze_heg__chuhai-phase"] = "除害",
  ["#ze_heg__zhangming-viewas"] = "义烈：你可将两张颜色相同的牌当做一张基本牌使用.",
  ["$ze_heg__chuhai1"] = "有我在此，安敢为害？！",
  ["$ze_heg__chuhai2"] = "小小孽畜，还不伏诛？！",
  ["$ze_heg__chuhai3"] = "此番不成，明日再战！",
  ["$ze_heg__zhangming1"] = "心怀远志，何愁声名不彰！",
  ["$ze_heg__zhangming2"] = "从今始学，成为有用之才！",
  ["~ze_heg__zhouchu"] = "改励自砥，誓除三害……",
}

local ze_heg__zhugeke = General(extension, "ze_heg__zhugeke", "wu", 3)
local duwu = fk.CreateActiveSkill{
  name = "ze_heg__duwu",
  frequency = Skill.Limited,
  anim_type = "offensive",
  target_num = 1,
  can_use = function(self, player)
    return player:getMark("@@ze_heg__duwu-turn") == 0
  end,
  card_filter = function(self, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) == Player.Hand and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= Self.id then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if target.hp == #selected_cards then
        if table.contains(selected_cards, Self:getEquipment(Card.SubtypeWeapon)) then
          return Self:distanceTo(target) == 1  --FIXME: some skills(eg.gongqi, meibu) add attackrange directly!
        else
          return Self:inMyAttackRange(target)
        end
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:throwCard(effect.cards, self.name, player, player)
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
    player:addSkillUseHistory(self.name, -1)
  end,
}
local duwu_trigger = fk.CreateTriggerSkill{
  name = "#ze_heg__duwu_trigger",
  mute = true,
  events = {fk.EnterDying, fk.AfterDying},
  can_trigger = function(self, event, target, player, data)
    return data.damage and data.damage.skillName == "ze_heg__duwu" and data.damage.from and data.damage.from == player and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.EnterDying then
      data.extra_data = data.extra_data or {}
      data.extra_data.ze_heg__duwu = player.id
    elseif data.extra_data.ze_heg__duwu == player.id and not target.dead then
      local room = player.room
      room:loseHp(player, 1, "ze_heg__duwu")
      room:handleAddLoseSkills(player,"-ze_heg__duwu", nil, true, true)
    end
  end,
}
duwu:addRelatedSkill(duwu_trigger)
ze_heg__zhugeke:addSkill("aocai")
ze_heg__zhugeke:addSkill(duwu)

Fk:loadTranslationTable{
  ["ze_heg__zhugeke"] = "诸葛恪",
  ["#ze_heg__zhugeke"] = "兴家赤族",
  ["designer:ze_heg__zhugeke"] = "韩旭",
  ["illustrator:ze_heg__zhugeke"] = "LiuHeng",
  ["aocai"] = "傲才",
  [":aocai"] = "当你于回合外需要使用或打出一张基本牌时，你可以观看牌堆顶的两张牌（若你没有手牌则改为四张），若你观看的牌中有此牌，你可以使用或打出之。",
  ["ze_heg__duwu"] = "黩武",
  [":ze_heg__duwu"] = "出牌阶段，你可以弃置X张手牌对你攻击范围内的一名其他角色造成1点伤害（X为该角色的体力值）。"..
  "若其因此进入濒死状态且被救回，则濒死状态结算后你失去1点体力，且你失去〖黩武〗。",
  ["#duwu_trigger"] = "黩武",
  ["$ze_heg__duwu1"] = "破曹大功，正在今朝！",
  ["$ze_heg__duwu2"] = "全力攻城！言退者，斩！",
  ["~ze_heg__zhugeke"] = "重权震主，是我疏忽了……",
}

local sunluban = General(extension, "ze_heg__sunluban", "wu", 3)  -- 孙鲁班
sunluban:addSkill("zenhui")
sunluban:addSkill("jiaojin")
S.generalInfo(sunluban, "孙鲁班", "为虎作伥", "官方", "官方", "官方", "官方", "本公主，何罪之有？")


local lukai = General(extension, "ze_heg__lukai", "wu", 3)
local ze_heg__xuanzhu = fk.CreateViewAsSkill{
  name = "ze_heg__xuanzhu",
  switch_skill_name = "ze_heg__xuanzhu",
  derived_piles = "ze_heg__xuanzhu",
  pattern = ".",
  interaction = function()
    local all_names = {}
    if 1==1 then
      all_names = U.getAllCardNames("b")
    else
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card:isCommonTrick() and not (card.is_derived or card.multiple_targets or card.is_passive) then
          table.insertIfNeed(all_names, card.name)
        end
      end
    end
    local names = U.getViewAsCardNames(Self, "ze_heg__xuanzhu", all_names)
    if #names > 0 then
      return UI.ComboBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:setMark("ze_heg__xuanzhu_subcards", cards)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local cards = use.card:getMark("ze_heg__xuanzhu_subcards")
    if Fk:getCardById(cards[1]).type == Card.TypeEquip then
      use.extra_data = use.extra_data or {}
      use.extra_data.ze_heg__xuanzhu_equip = true
    end
    player:addToPile(self.name, cards, true, self.name)
  end,
  after_use = function(self, player, use)
    if player.dead then return end
    if use.extra_data and use.extra_data.ze_heg__xuanzhu_equip then
      local cards = player:getPile(self.name)
      if #cards > 0 then
        player.room:askForDiscard(player, 1, 1, true, self.name, false)
      end
    else
      player.room:askForDiscard(player, 1, 1, true, self.name, false)
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
  enabled_at_response = function(self, player, response)
    if not response and player:usedSkillTimes(self.name) == 0 and Fk.currentResponsePattern then
      local all_names = {}
      if 1==1 then
        all_names = U.getAllCardNames("b")
      else
        for _, id in ipairs(Fk:getAllCardIds()) do
          local card = Fk:getCardById(id)
          if card:isCommonTrick() and not (card.is_derived or card.multiple_targets or card.is_passive) then
            table.insertIfNeed(all_names, card.name)
          end
        end
      end
      return #U.getViewAsCardNames(player, "ze_heg__xuanzhu", all_names) > 0
    end
  end,
}
lukai:addSkill(ze_heg__xuanzhu)
lukai:addSkill("jiane")
lukai:addCompanions("zhonger_lord_heg__lukang")
Fk:loadTranslationTable{
  ["ze_heg__lukai"] = "陆凯",
  ["#ze_heg__lukai"] = "节概梗梗",
  ["ze_heg__xuanzhu"] = "玄注",
  [":ze_heg__xuanzhu"] = "每回合限一次，你可以将一张牌移出游戏，视为使用任意基本牌，然后你弃置一张牌。",
  ["$ze_heg__xuanzhu1"] = "提笔注太玄，佐国定江山。",
  ["$ze_heg__xuanzhu2"] = "总太玄之要，纵弼国之实。",
  ["~ze_heg__lukai"] = "注经之人，终寄身于土……",
}

local liuzan = General(extension, "ze_heg__liuzan", "wu", 4)
local function searchze_heg__fenyinCards(room, findOne)
  if #room.discard_pile == 0 then return {} end
  local ids = {}
  local discard_pile = table.simpleClone(room.discard_pile)
  local logic = room.logic
  local events = logic.event_recorder[GameEvent.MoveCards] or Util.DummyTable
  for i = #events, 1, -1 do
    local e = events[i]
    for _, move in ipairs(e.data) do
      for _, info in ipairs(move.moveInfo) do
        local id = info.cardId
        if table.removeOne(discard_pile, id) then
          if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse then
            table.insertIfNeed(ids, id)
            if findOne then
              return ids
            end
          end
        end
      end
    end
    if #discard_pile == 0 then break end
  end
  return ids
end
local fenyin = fk.CreateTriggerSkill{
  name = "ze_heg__fenyin",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if player.phase == Player.Start then
        return #searchze_heg__fenyinCards(player.room, true) > 0
      elseif player.phase == Player.Finish then
        return player:isKongcheng() or
          table.every(player.player_cards[Player.Hand], function(id) return Fk:getCardById(id):getMark("@@ze_heg__fenyin-inhand-turn") == 0 end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.phase == Player.Start then
      return player.room:askForSkillInvoke(player, self.name, nil, "#ze_heg__fenyin-invoke:::"..player.hp)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if player.phase == Player.Start then
      local room = player.room
      local cards = searchze_heg__fenyinCards(room, false)
      if #cards == 0 then return false end
      table.sort(cards, function (a, b)
        local cardA, cardB = Fk:getCardById(a), Fk:getCardById(b)
        if cardA.type == cardB.type then
          if cardA.sub_type == cardB.sub_type then
            if cardA.name == cardB.name then
              return a > b
            else
              return cardA.name > cardB.name
            end
          else
            return cardA.sub_type < cardB.sub_type
          end
        else
          return cardA.type < cardB.type
        end
      end)
      local get = room:askForCardsChosen(player, player, 1, 1, {
        card_data = {
          { "pile_discard", cards }
        }
      }, self.name, "#ze_heg__fenyin-choose:::" .. tostring(player.hp))
      room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, self.name, "", false, player.id, "@@ze_heg__fenyin-inhand-turn")
    else
      player:drawCards(0, self.name)
    end
  end,
}
liuzan:addSkill(fenyin)
Fk:loadTranslationTable{
  ["ze_heg__liuzan"] = "留赞",
  ["#ze_heg__liuzan"] = "啸天亢声",
  ["ze_heg__fenyin"] = "奋音",
  [":ze_heg__fenyin"] = "准备阶段，你可以从弃牌堆中获得至多一张不因使用而进入弃牌堆的牌。",
  ["#ze_heg__fenyin-invoke"] = "奋音：你可以获得弃牌堆中至多%arg张不因使用而进入弃牌堆的牌",
  ["#ze_heg__fenyin-choose"] = "奋音：从弃牌堆中挑选至多%arg张卡牌获得",
  ["@@ze_heg__fenyin-inhand-turn"] = "奋音",
  ["$ze_heg__fenyin1"] = "吾军杀声震天，则敌心必乱！",
  ["$ze_heg__fenyin2"] = "阵前亢歌，以振军心！",
  ["~ze_heg__liuzan"] = "若因病困此，命矣。",
}







local ze_heg__yanyan = General(extension, "ze_heg__yanyan", "qun", 4)
local ze_heg__juzhan = fk.CreateTriggerSkill{
  name = "ze_heg__juzhan",
  switch_skill_name = "ze_heg__juzhan",
  events = { fk.TargetSpecified, fk.TargetConfirmed },
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self) and
      data.card.trueName == 'slash') then return end
    if event == fk.TargetConfirmed then
      return player.id ~= data.from
    elseif event == fk.TargetSpecified  then
      return not player.room:getPlayerById(data.to):isNude()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    local aim = data ---@type AimStruct

    if event == fk.TargetConfirmed then
      local from = room:getPlayerById(aim.from)
      player:drawCards(1, self.name)
      from:drawCards(1, self.name)
      local x = from:getMark("@@ze_heg__juzhan-turn")
      if x == 0 then x = {} end
      table.insert(x, player.id)
      room:setPlayerMark(from, "@@ze_heg__juzhan-turn", x)
    else
      local to = room:getPlayerById(aim.to)
      local from = player
      player:drawCards(1, self.name)
      local x = from:getMark("@@ze_heg__juzhan-turn")
      if x == 0 then x = {} end
      table.insert(x, to.id)
      room:setPlayerMark(from, "@@ze_heg__juzhan-turn", x)
    end
  end,
}
local ze_heg__juzhan_prohibit = fk.CreateProhibitSkill{
  name = "#ze_heg__juzhan_prohibit",
  is_prohibited = function(self, from, to)
    local x = from:getMark("@@ze_heg__juzhan-turn")
    if x == 0 then return false end
    return table.contains(x, to.id)
  end,
}
ze_heg__juzhan:addRelatedSkill(ze_heg__juzhan_prohibit)
ze_heg__yanyan:addSkill(ze_heg__juzhan)
Fk:loadTranslationTable{
  ["ze_heg__yanyan"] = "严颜",
  ["#ze_heg__yanyan"] = "断头将军",
  ["illustrator:ze_heg__yanyan"] = "Town",
  ["ze_heg__juzhan"] = "拒战",
  [":ze_heg__juzhan"] = "当你成为其他角色使用【杀】的目标后，你可以与其各摸一张牌，然后其本回合不能再对你使用牌。当你使用【杀】指定一名角色为目标后，你可以摸一张牌，然后你本回合不能再对其使用牌。",
  ["@@ze_heg__juzhan-turn"] = "拒战",
  ["~ze_heg__yanyan"] = "宁可断头死，安能屈膝降！",
  ["$ze_heg__juzhan1"] = "砍头便砍头，何为怒耶！",
  ["$ze_heg__juzhan2"] = "我州但有断头将军，无降将军也！",
}

local huaxiong = General(extension, "ze_heg__huaxiong", "qun", 4)
local ze_heg__shizhan = fk.CreateActiveSkill{
  name = "ze_heg__shizhan",
  anim_type = "offensive",
  prompt = "#ze_heg__shizhan-active",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and not Fk:currentRoom():getPlayerById(to_select):isProhibited(Self, Fk:cloneCard("duel"))
  end,
  target_num = 1,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:useVirtualCard("duel", nil, target, player, self.name, true)
  end,
}
local ze_heg__yaowu = fk.CreateTriggerSkill{
  name = "ze_heg__yaowu",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if player.phase == Player.NotActive then return false end
    return target == player and player:hasSkill(self) and data.card
  end,
  on_use = function(self, event, target, player, data)
    if data.card.color ~= Card.Red then
      player:drawCards(2, self.name)
    else
      player:drawCards(2, self.name)
    end
  end,
}
ze_heg__shizhan:addRelatedSkill(ze_heg__yaowu)
huaxiong:addSkill(ze_heg__shizhan)
Fk:loadTranslationTable{
  ["ze_heg__huaxiong"] = "华雄",
  ["ze_heg__yaowu"] = "势斩",
  ["ze_heg__shizhan"] = "势斩",
  [":ze_heg__shizhan"] = "当你于回合内受到牌造成的伤害后，你摸两张牌。出牌阶段限一次，你可以令一名其他角色视为对你使用【决斗】。",
  ["#ze_heg__shizhan-active"] = "发动 势斩，选择一名其他角色，令其视为对你使用【决斗】",
  ["$ze_heg__shizhan1"] = "看你能坚持几个回合！",
  ["$ze_heg__shizhan2"] = "兀那汉子，且报上名来！",
  ["~ze_heg__huaxiong"] = "我掉以轻心了……",
}

local duyu = General(extension, "ze_heg__duyu", "qun", 4)
duyu.mainMaxHpAdjustedValue = -1
local sanchen = fk.CreateActiveSkill{
  name = "ze_heg__sanchen",
  anim_type = "drawcard",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getPlayerById(to_select):getMark("sanchen-turn") == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    if player:hasSkill("zhaotao", true) and player:usedSkillTimes("zhaotao", Player.HistoryGame) == 0 then
      room:addPlayerMark(player, "@ze_heg__sanchen")
    end
    room:addPlayerMark(target, "sanchen-turn", 1)
    target:drawCards(3, self.name)
    local cards = room:askForDiscard(target, 3, 3, true, self.name, false, ".", "#sanchen_discard:"..player.id)
    local typeMap = {}
    for _, id in ipairs(cards) do
      typeMap[tostring(Fk:getCardById(id).type)] = (typeMap[tostring(Fk:getCardById(id).type)] or 0) + 1
    end
    for _, v in pairs(typeMap) do
      if v >= 2 then return end
    end
    target:drawCards(1, self.name)
    player:setSkillUseHistory(self.name, 0, Player.HistoryPhase)
  end,
}
local ze_heg__miewu = fk.CreateViewAsSkill{
  name = "ze_heg__miewu",
  anim_type = "offensive",
  relate_to_place = "m",
  pattern = "unexpectation",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local c = Fk:cloneCard("unexpectation")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name) == 0
  end,
}
duyu:addSkill(sanchen)
duyu:addSkill(ze_heg__miewu)
Fk:loadTranslationTable{
  ["ze_heg__duyu"] = "杜预",
  ["#ze_heg__duyu"] = "文成武德",
  ["illustrator:ze_heg__duyu"] = "鬼画府",
  [":ze_heg__sanchen"] = "出牌阶段限一次，你可令一名角色摸三张牌然后弃置三张牌。若其未因此次〖三陈〗的效果而弃置至少两张类别相同的牌，则其摸一张牌，且本技能视为未发动过（本回合不能再指定其为目标）。",
  ["ze_heg__miewu"] = "破竹",
  [":ze_heg__miewu"] = "主将技，此武将牌上的单独阴阳鱼个数-1。出牌阶段限一次，你可将一张手牌当【出其不意】使用。",
  ["#sanchen_discard"] = "三陈：弃置三张牌，若类别各不相同则你摸一张牌且 %src 可以再发动“三陈”",
  ["ze_heg__sanchen"] = "三陈",
  ["@ze_heg__sanchen"] = "三陈",
  ["$ze_heg__sanchen1"] = "贼计已穷，陈兵吴地，可一鼓而下也。",
  ["$ze_heg__sanchen2"] = "伐吴此举，十有九利，惟陛下察之。",
  ["$ze_heg__miewu1"] = "倾荡之势已成，石城尽在眼下。",
  ["$ze_heg__miewu2"] = "吾军势如破竹，江东六郡唾手可得。",
  ["~ze_heg__duyu"] = "洛水圆石，遂道南向，吾将以俭自完耳……",
}

local yanghu = General(extension, "ze_heg__yanghu", "qun", 3)
local mingfa = fk.CreateActiveSkill{
  name = "ze_heg__mingfa",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function (self, to_select, selected)
    return false
  end,
  target_filter = function (self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and (not H.compareKingdomWith(Fk:currentRoom():getPlayerById(to_select), Self))
  end,
  on_use = function(self, room, effect)
    local target = room:getPlayerById(effect.tos[1])
    local mark = type(target:getMark("@@ze_heg__mingfa_delay")) == "table" and target:getMark("@@ze_heg__mingfa_delay") or {}
    table.insert(mark, effect.from)
    room:setPlayerMark(target, "@@ze_heg__mingfa_delay", mark)
  end,
}

local mingfa_delay = fk.CreateTriggerSkill{
  name = "#ze_heg__mingfa_delay",
  anim_type = "offensive",
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    if target.dead or player.dead then return false end
    return table.contains(U.getMark(target, "@@ze_heg__mingfa_delay"), player.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player:getHandcardNum() >= target:getHandcardNum() then
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = self.name,
      }
      if target:getHandcardNum() > 0 then
        local cards = room:askForCardsChosen(player, target, 1, 1, "h", self.name)
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(cards)
        room:obtainCard(player, dummy, false, fk.ReasonPrey)
      end
    elseif player:getHandcardNum() < target:getHandcardNum() then
      player:drawCards(math.min(target:getHandcardNum() - player:getHandcardNum(), 5), self.name)
    end
  end,

  refresh_events = {fk.AfterTurnEnd, fk.BuryVictim},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterTurnEnd then
      return player == target and player:getMark("@@ze_heg__mingfa_delay") ~= 0
    elseif event == fk.BuryVictim then
      local mark = player:getMark("@@ze_heg__mingfa_delay")
      return type(mark) == "table" and table.every(player.room.alive_players, function (p)
        return not table.contains(mark, p.id)
      end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@ze_heg__mingfa_delay", 0)
  end,
}
mingfa:addRelatedSkill(mingfa_delay)
yanghu:addSkill(mingfa)
yanghu:addSkill("rongbei")
yanghu:addCompanions("ze_heg__duyu")
Fk:loadTranslationTable{
  ["ze_heg__yanghu"] = "羊祜",
  ["#ze_heg__yanghu"] = "执德清劭",
  ["illustrator:ze_heg__yanghu"] = "白",
  ["ze_heg__mingfa"] = "明伐",
  [":ze_heg__mingfa"] = "出牌阶段限一次，你可以选择与你势力不同或未确定势力的一名其他角色，其下个回合结束时，若其手牌数：不大于你，你对其造成1点伤害并获得其一张手牌；"..
  "大于你，你摸至与其手牌数相同（最多摸五张）。",
  ["#ze_heg__mingfa_delay"] = "明伐",
  ["@@ze_heg__mingfa_delay"] = "明伐",
  ["$ze_heg__mingfa1"] = "明日即为交兵之时，望尔等早做准备。",
  ["$ze_heg__mingfa2"] = "吾行明伐之策，不为掩袭之计。",
  ["~ze_heg__yanghu"] = "此生所憾，唯未克东吴也……",
}

local laiyinger = General(extension, "ze_heg__laiyinger", "qun", 3, 3, General.Female)
local ze_heg__xiaowu__amazingGraceSkill = fk.CreateActiveSkill{
  name = "ze_heg__xiaowu__amazing_grace_skill",
  prompt = "#amazing_grace_skill",
  can_use = Util.GlobalCanUse,
  on_use = Util.GlobalOnUse,
  mod_target_filter = Util.TrueFunc,
  on_action = function(self, room, use, finished)
    local player = room:getPlayerById(use.from)
    if not finished then
      local toDisplay = player:getCardIds(Player.Hand)
      room:moveCardTo(toDisplay, Card.Processing, nil, fk.ReasonJustMove, "amazing_grace_skill", "", true, player.id)

      table.forEach(room.players, function(p)
        room:fillAG(p, toDisplay)
      end)

      use.extra_data = use.extra_data or {}
      use.extra_data.AGFilled = toDisplay
    else
      if use.extra_data and use.extra_data.AGFilled then
        table.forEach(room.players, function(p)
          room:closeAG(p)
        end)

        local toDiscard = table.filter(use.extra_data.AGFilled, function(id)
          return room:getCardArea(id) == Card.Processing
        end)

        if #toDiscard > 0 then
          if player.dead then
            room:moveCards({
              ids = toDiscard,
              toArea = Card.DiscardPile,
              moveReason = fk.ReasonPutIntoDiscardPile,
            })
          else
            room:moveCardTo(toDiscard, Card.PlayerHand, player, fk.ReasonJustMove, "amazing_grace_skill", "", true, player.id)
          end
        end
      end

      use.extra_data.AGFilled = nil
    end
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if not (effect.extra_data and effect.extra_data.AGFilled and #effect.extra_data.AGFilled > 0) then
      return
    end

    local chosen = room:askForAG(to, effect.extra_data.AGFilled, false, "amazing_grace_skill")
    room:takeAG(to, chosen, room.players)
    room:obtainCard(effect.to, chosen, true, fk.ReasonPrey)
    table.removeOne(effect.extra_data.AGFilled, chosen)
  end,
}
Fk:addSkill(ze_heg__xiaowu__amazingGraceSkill)
local ze_heg__xiaowu = fk.CreateActiveSkill{
  name = "ze_heg__xiaowu",
  target_num = 1,
  card_num = 0,
  prompt = "#ze_heg__xiaowu-active",
  anim_type = "control",
  interaction = function()
    return UI.ComboBox {
      choices = {"draw2", "ze_heg__xiaowu_equip"}
    }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    if self.interaction.data == "draw2" then
      target:drawCards(2, self.name)
    else
      local cards = {}
      for i = 1, #room.draw_pile, 1 do
        local card = Fk:getCardById(room.draw_pile[i])
        if card.type == Card.TypeEquip and target:canUseTo(card, target) then
          table.insertIfNeed(cards, card)
        end
      end
      if #cards > 0 then
        room:useCard{
          from = target.id,
          card = cards[math.random(1, #cards)],
          tos = {{target.id}},
        }
      end
    end
    if target.dead or target:getHandcardNum() < #room.alive_players then return end
      room:loseHp(target, 1, self.name)
  end,
}
laiyinger:addSkill(ze_heg__xiaowu)
local ze_heg__huaping = fk.CreateTriggerSkill{
  name = "ze_heg__huaping",
  anim_type = "support",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and H.compareKingdomWith(target, player) and
      player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForCard(player, 1, 999, true, self.name, true, ".", "#ze_heg__huaping-invoke::"..target.id)
    if #cards >= 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local dummy = Fk:cloneCard("dilu")
    dummy:addSubcards(self.cost_data)
    player.room:obtainCard(target, dummy, false, fk.ReasonGive, player.id)
    return true
  end,
}
laiyinger:addSkill(ze_heg__huaping)
Fk:loadTranslationTable{
  ["ze_heg__laiyinger"] = "来莺儿",
  ["#ze_heg__laiyinger"] = "余音绕雀台",
  ["illustrator:laiyinger"] = "君桓文化",
  ["ze_heg__xiaowu"] = "绡舞",
  [":ze_heg__xiaowu"] = "出牌阶段限一次，你可以令一名角色摸两张牌或使用牌堆中的一张随机装备牌。若其手牌数不小于存活角色数，"..
  "其失去1点体力。",
  ["#ze_heg__xiaowu-active"] = "发动 绡舞，选择一名角色",
  ["ze_heg__xiaowu_equip"] = "使用装备",
  ["ze_heg__huaping"] = "化萍",
  [":ze_heg__huaping"] = "每轮限一次，当一名与你势力相同的角色受到伤害时，你可以交给其任意张牌并防止此伤害。",
  ["#ze_heg__huaping-invoke"] = "化萍：你可以交给 %dest 至少一张牌，防止其受到的伤害",
  ["$ze_heg__xiaowu1"] = "繁星临云袖，明月耀舞衣。",
  ["$ze_heg__xiaowu2"] = "逐舞飘轻袖，传歌共绕梁。",
  ["$ze_heg__huaping1"] = "风絮飘残，化萍而终。",
  ["$ze_heg__huaping2"] = "莲泥刚倩，藕丝萦绕。",
  ["~ze_heg__laiyinger"] = "谷底幽兰艳，芳魂永留香……",
}














return extension